package cn.lili.controller.user;

import cn.lili.common.utils.ResultUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.common.vo.ResultMessage;
import cn.lili.modules.permission.entity.dos.Role;
import cn.lili.modules.permission.entity.dto.RoleEditDTO;
import cn.lili.modules.permission.integration.PermissionIntegrationService;
import cn.lili.modules.permission.service.RoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 角色管理接口
 *
 * @author Chopper
 * @since 2020/11/20 18:50
 */
@RestController
@Api(tags = "角色管理接口")
@RequestMapping("/user/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    private final PermissionIntegrationService permissionIntegrationService;

    @PostMapping
    @ApiOperation(value = "添加角色")
    public ResultMessage<Object> add(@RequestBody RoleEditDTO roleEditDTO) {
        roleService.saveRole(roleEditDTO);
        return ResultUtil.success();
    }

    @GetMapping
    @ApiOperation(value = "分页获取角色")
    public ResultMessage<Page> page(PageVO pageVo, Role role) {
        Page page = roleService.pageRole(pageVo, role);
        return ResultUtil.data(page);
    }

    @GetMapping(value = "/{scene}/{extendId}")
    @ApiOperation(value = "分页获取角色,用于跨平台语言")
    public ResultMessage<Page> page(PageVO pageVo, Role role, @PathVariable String scene, @PathVariable String extendId) {

        role.setExtendId(extendId);
        role.setScene(scene);
        Page page = roleService.pageRole(pageVo, role);
        return ResultUtil.data(page);
    }

    @PutMapping("/{roleId}")
    @ApiOperation(value = "编辑角色")
    public ResultMessage<Object> edit(@PathVariable String roleId, @RequestBody RoleEditDTO roleEditDTO) {
        roleEditDTO.getRole().setId(roleId);
        roleService.updateRoleById(roleEditDTO);
        return ResultUtil.success();
    }

    @DeleteMapping(value = "/{ids}")
    @ApiOperation(value = "批量删除")
    public ResultMessage<Role> delByIds(@PathVariable List<String> ids) {
        permissionIntegrationService.deleteRoles(ids);
        return ResultUtil.success();
    }


}
